package cn.com.yto56.cainiao.exponent.es.service;

import cn.com.yto56.basic.framework.core.util.DateUtil;
import cn.com.yto56.cainiao.exponent.common.Constants;
import cn.com.yto56.cainiao.exponent.dto.AjaxResponse;
import cn.com.yto56.cainiao.exponent.dto.RestResponse;
import cn.com.yto56.cainiao.exponent.elasticsearch.service.FrontHandleService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.frameworkset.elasticsearch.entity.MapRestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Service("frontHandleServiceProxyImpl")
public class FrontHandleServiceProxyImpl implements FrontHandleService {
    private static final String FRONT_SERVICE = "cn-exponet-es-front-ms";
    private LoadBalancerClient loadBalancer;
    private ServiceInstance frontHandleService;

    @Autowired
    public FrontHandleServiceProxyImpl(LoadBalancerClient loadBalancer) {
        this.loadBalancer = loadBalancer;
        frontHandleService = this.loadBalancer.choose(FRONT_SERVICE);
    }

    @Override
    public MapRestResponse query(String path, String entity) {
        return null;
    }

    @Override
    public String queryInbound(String mailNo) {
        ResponseEntity<String> responseEntity = new RestTemplate().getForEntity(frontHandleService.getUri() + "/front/queryInbound/" + mailNo, String.class);
        return responseEntity.getBody();
    }

    @Override
    public Date queryCanvassTime(String mailNo) {
        ResponseEntity<String> responseEntity = new RestTemplate().getForEntity(frontHandleService.getUri() + "/front/queryCanvassTime/" + mailNo, String.class);
        RestResponse<Date> response = JSON.parseObject(responseEntity.getBody(), new TypeReference<RestResponse<Date>>() {
        });
        return response.getData();

    }
}
